package com.example.service.impl;

import com.example.entity.Talk;
import com.example.entity.User;
import com.example.entity.vo.TalkVo;
import com.example.mapper.TalkMapper;
import com.example.service.TalkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;


@Service
public class TalkServiceImpl implements TalkService {

    @Autowired
    TalkMapper talkMapper;

    @Override
    public int insert(Talk record) {
        return talkMapper.insert(record);
    }

    @Override
    public CopyOnWriteArrayList<TalkVo> selectBytouser(Integer touser) {
        CopyOnWriteArrayList<TalkVo> talkVos = talkMapper.selectBytouser(touser);
        return talkVos;
    }

    @Override
    public int delByPrimKey(Integer talkid) {
        return talkMapper.deleteByPrimaryKey(talkid);
    }

    @Override
    public int updateByPrimaryKeySelective(Talk record) {
        return talkMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public void unreadToRead(Integer talkid) {
        Talk talk = new Talk();
        talk.setTalkid(talkid);
        talk.setState("A");
        talkMapper.updateByPrimaryKeySelective(talk);
    }

    @Override
    public List<User> selectByMyself(Integer touser) {
        List<User> users = talkMapper.selectByMyself(touser);

        List<User> unique = users.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getId))), ArrayList::new)
        );
        return unique;
    }
}
